package com.studyadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.studyadmin.entity.Role;
import com.studyadmin.entity.Student;
import com.studyadmin.entity.Teacher;
import com.studyadmin.mapper.RoleMapper;
import com.studyadmin.mapper.StudentMapper;
import com.studyadmin.mapper.TeacherMapper;
import com.studyadmin.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyadmin.util.JsonUtil;
import com.studyadmin.dto.RegParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.studyadmin.util.EncryptUtils.desEncrypt;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author lyl
 * @since 2023-03-15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    @Autowired(required = false)
    RoleMapper roleMapper;
    @Autowired(required = false)
    StudentMapper studentMapper;
    @Autowired(required = false)
    TeacherMapper teacherMapper;

    /**
     * 登录
     * @param regParam 用户学号，密码
     * @return 查询结果 角色学号 角色编码 1学生 2老师
     */
    @Override
    public JsonUtil login(RegParam regParam) {
        System.out.println("--------------"+regParam);
        JsonUtil jsonUtil = new JsonUtil();

       Role role = roleMapper.selectOne(
                new QueryWrapper<Role>()
                        .select("role_num","role_code")
                        .eq("role_num",regParam.getStuNum())
                        .eq("is_deleted",0)
        );
        if(role==null){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("该用户账号不存在");
            return jsonUtil;
        }

        //登录的是学生账号
        if(role.getRoleCode().equals("1")){
            Student student = studentMapper.selectOne(
                    new QueryWrapper<Student>()
                            .select("stu_num").select("password")
                            .eq("stu_num",regParam.getStuNum())
                            .eq("del_flag",0)
            );
            if(student==null){
                jsonUtil.setCode(1);
                jsonUtil.setMsg("该用户账号不存在");
                return jsonUtil;
            }
            String p=null;
            try {
                //编码
                p=desEncrypt(regParam.getPassword());
            } catch (Exception e) {
                e.printStackTrace();
            }
            //密码错误
            if( p == null || !p.equals(student.getPassword())) {
                jsonUtil.setCode(1);
                jsonUtil.setMsg("密码错误");
                return jsonUtil;
            }else {
                jsonUtil.setCode(0);
                jsonUtil.setMsg("登录成功");
                jsonUtil.setData(role);
                return jsonUtil;
            }
        }else if(role.getRoleCode().equals("2")){
            Teacher teacher = teacherMapper.selectOne(
                    new QueryWrapper<Teacher>()
                            .select("tea_num").select("password")
                            .eq("tea_num",regParam.getStuNum())
                            .eq("del_flag",0)
            );
            if(teacher==null){
                jsonUtil.setCode(1);
                jsonUtil.setMsg("该用户账号不存在");
                return jsonUtil;
            }
            String p=null;
            try {
                //编码
                p=desEncrypt(regParam.getPassword());
            } catch (Exception e) {
                e.printStackTrace();
            }
            //密码正确

            if(!p.equals(teacher.getPassword())){
                jsonUtil.setCode(1);
                jsonUtil.setMsg("密码错误");
                return jsonUtil;
            }else {
                jsonUtil.setCode(0);
                jsonUtil.setMsg("登录成功");
                jsonUtil.setData(role);
                return jsonUtil;
            }
        }else {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("未知错误");
            return jsonUtil;
        }
    }
}
